package com.yq.order.http;

import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2017/10/13.
 */

public class RetrofitManager {

    private final String BASE_URL = "http://www.ems.com.cn/";
    private static RetrofitManager mInstance = null;
    private Retrofit retrofit = null;
    private Retrofit retrofitNew = null;
    private static HttpService httpService;

    public static RetrofitManager getInstance() {
        if (null == mInstance)
            mInstance = new RetrofitManager();
        return mInstance;
    }

    private RetrofitManager() {
    }

    public static HttpService getHttpService() {
        if (null == httpService)
            httpService = getDefalutRetrofit().create(HttpService.class);
        return httpService;
    }

    public static Retrofit getDefalutRetrofit() {
        return getInstance().getRetrofit();
    }

    /**
     * 获取 retrofi 实例
     *
     * @return
     */
    public Retrofit getRetrofit() {
        if (null == retrofit)
            retrofit = new Retrofit.Builder()
                    .client(OkHttpClientManager.getClient())
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        return retrofit;
    }

    /**
     * baseUrl 有变化时调用此方法
     *
     * @param newBaseUrl 新的 baseUrl
     * @return
     */
    public Retrofit getNewRetrofit(String newBaseUrl) {
        retrofitNew = new Retrofit.Builder()
                .baseUrl(newBaseUrl)
                .build();
        return retrofitNew;
    }

}
